﻿using KongMing.WebTools.Models;
using System.Text;
using System.Text.RegularExpressions;

namespace KongMing.WebTools.Common
{
    public static class Tools
    {
        private static Regex regexOfSplit = new Regex("([A-Z]+)");
        /// <summary>
        /// 字段用下划线分割 UserName => User_Name
        /// </summary>
        public static string SplitWords(this string words)
        {
            var newword = regexOfSplit.Replace(words, "_$1");
            newword = newword.Trim('_');
            return newword;
        }

        public static void AppendLineLv(this StringBuilder sb, string str, int marginLv = 1)
        {
            var margin = new StringBuilder("");
            for (int i = 1; i <= marginLv; i++)
            {
                margin.Append("    ");
            }
            sb.AppendLine(margin.ToString() + str);
        }

        public static void AppendRemark(this StringBuilder sb, string str, int marginLv = 1)
        {
            sb.AppendLineLv("/// <summary>", marginLv);
            sb.AppendLineLv($"/// {str}", marginLv);
            sb.AppendLineLv("/// </summary>", marginLv);
        }

        /// <summary>
        /// 将SqlServer数据类型转为C#数据类型
        /// </summary>
        /// <param name="sqlType">sql数据类型</param>
        /// <param name="isNullable">是否允许为空</param>
        public static string SqlTypeToCsType(this Field field)
        {
            string csType = field.DataType switch
            {
                SqlType.Varchar => CShapType.String,
                SqlType.Nvarchar => CShapType.String,
                SqlType.Text => CShapType.String,
                SqlType.Int => CShapType.Int,
                SqlType.Decimal => CShapType.Decimal,
                SqlType.DateTime => CShapType.DateTime,
                _ => throw new Exception($"'{field.DataType}'数据类型不支持！")
            };
            if (field.IsNullable)
                csType += "?";
            return csType;
        }

        /// <summary>
        /// 获取C#字段默认值
        /// </summary>
        public static void FixCSharpDefaultValue(this Field field)
        {
            if (field.IsPrimaryKey)
                return;
            field.CSharpDefaultValue = field.DataType switch
            {
                SqlType.Varchar => string.IsNullOrEmpty(field.DefaultValue) ? " = \"\";" : $" = \"{field.DefaultValue}\";",
                SqlType.Nvarchar => string.IsNullOrEmpty(field.DefaultValue) ? " = \"\";" : $" = \"{field.DefaultValue}\";",
                SqlType.Text => string.IsNullOrEmpty(field.DefaultValue) ? " = \"\";" : $" = \"{field.DefaultValue}\";",
                SqlType.DateTime => string.IsNullOrEmpty(field.DefaultValue) ? $" = DateTime.MinValue;" : $" = DateTime.Parse(\"{field.DefaultValue}\");",
                _ => ""
            };
        }

        public static string FixSqlDataType(string type, string length)
        {
            string str_dataType = "";
            if (type == SqlType.Decimal)
            {
                if (string.IsNullOrEmpty(length))
                    str_dataType = "decimal(18,2)";
                else
                {
                    var arr = length.Split(',');
                    if (arr.Length == 2)
                        str_dataType = $"decimal({arr[0]},{arr[1]})";
                    else
                        str_dataType = "decimal(18,2)";
                }
            }
            else if (type == SqlType.Varchar)
            {
                str_dataType = SqlType.Varchar + $"({length})";
            }
            else if (type == SqlType.Nvarchar)
            {
                str_dataType = SqlType.Nvarchar + $"({length})";
            }
            else
            {
                str_dataType = type;
            }
            return str_dataType;
        }

        /// <summary>
        /// 格式化sql默认值
        /// </summary>
        public static void FixSqlDefaultValue(this Field field)
        {
            if (!field.IsPrimaryKey && !field.IsIdentity)
            {
                field.SqlDefaultValue = field.DataType switch
                {
                    SqlType.Varchar => string.IsNullOrEmpty(field.DefaultValue) ? "" : field.DefaultValue,
                    SqlType.Nvarchar => string.IsNullOrEmpty(field.DefaultValue) ? "" : field.DefaultValue,
                    SqlType.Text => string.IsNullOrEmpty(field.DefaultValue) ? "" : field.DefaultValue,
                    SqlType.Int => string.IsNullOrEmpty(field.DefaultValue) ? "0" : field.DefaultValue,
                    SqlType.Decimal => string.IsNullOrEmpty(field.DefaultValue) ? "0" : field.DefaultValue,
                    SqlType.DateTime => string.IsNullOrEmpty(field.DefaultValue) ? null : field.DefaultValue,
                    _ => throw new Exception($"'{field.DataType}'数据类型不存在")
                };
            }
        }
    }
}
